package ATMproject;

import jframe.LoginPanel;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Date;
import java.io.*;
import java.util.List;

public class MainPage extends JFrame {
    //声明其他跳转类
    private Login login = new Login(this);
    private Menu menu = new Menu(this);
    private Save save = new Save(this);
    private Draw draw = new Draw(this);
    private Inquire inquire = new Inquire(this);
    private Transform transform = new Transform(this);
    private Change change = new Change(this);


    //设地址为常量
    public static final String PATH = "users.dat";


    public Inquire getInquire() {
        return inquire;
    }

    public void setInquire(Inquire inquire) {
        this.inquire = inquire;
    }

    //声明布局类
    private CardLayout cardLayout = new CardLayout();

    //加背景图
    private Image backgroundimg = Util.getImage("/atmimage/background.jpg");

    //用户及其get和set方法
    private ArrayList<User> userarraylist;

    public ArrayList<User> getUserarraylist() {
        return userarraylist;
    }

    public void setUserarraylist(ArrayList<User> userarraylist) {
        this.userarraylist = userarraylist;
    }

    //取得下标
    private int index;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    //声明用户对象
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    private SqlUtil sqlUtil;

    public SqlUtil getSqlUtil() {
        return sqlUtil;
    }

    public void setSqlUtil(SqlUtil sqlUtil) {
        this.sqlUtil = sqlUtil;
    }

    //构造函数
    public MainPage() {
        //调用初始化函数
        init();
//        Util.writeObject("D:/4.txt", userarraylist);
//        Util.readObject("D:/4.txt");

        this.setContentPane(new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(backgroundimg, 0, 0, 800, 600, this);
            }
        });

        this.setLayout(cardLayout);

        //传其他页面
        this.add("login", login);
        this.add("menu", menu);
        this.add("save", save);
        this.add("draw", draw);
        this.add("inquire", inquire);
        this.add("transform", transform);
        this.add("change", change);

        this.setSize(800, 600);
        this.setTitle("主界面");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    //初始化用户集合
    public void init() {

//        userarraylist = (ArrayList) FileUtil.readObject(PATH);
//        if (userarraylist == null) {
//            userarraylist = new ArrayList<>();
//
//            userarraylist.add(new User("a", "111", 500));
//            userarraylist.add(new User("b", "222", 1000));
//            userarraylist.add(new User("c", "333", 1500));
//
//            FileUtil.writeObject(PATH, userarraylist);
//        }

        List userlist = sqlUtil.queryMethod();

    }


    //换页面的方法,
    public void switchPanel(String name) {
        cardLayout.show(this.getContentPane(), name);
    }

    public static void main(String[] args) {
        new MainPage();
    }
}
